package ecoiot.com.testproject.chart;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import ecoiot.com.testproject.R;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.util.ChartUtils;
import lecho.lib.hellocharts.view.LineChartView;

public class HelloChartActivity extends AppCompatActivity {

    private DemoLineChartView chartView;
    private LineChartData lineData;
    private Line line;
    private List<PointValue> values;
    private List<AxisValue> mValuesAxisX;
    private List<AxisValue> mValuesAxisY;
    private Axis axisX;
    private Axis axisY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello_chart);

        chartView = (DemoLineChartView) findViewById(R.id.hello_line_chart_view);

        initLine();


//        final Viewport v = new Viewport(chartView.getMaximumViewport());
//        v.bottom = 5;          //防止曲线超过范围做边界保护
//        v.top = 95;            //根据具体需求设置 建议设置一下
//        chartView.setMaximumViewport(v);                   //设置最大视图
//        v.top = 100;
//        v.bottom = -10;
//        chartView.setCurrentViewport(v);

    }

    /**
     * 初始化曲线的属性
     */
    void initLine() {
        values = new ArrayList<>();
        mValuesAxisX = new ArrayList<>();
        mValuesAxisY = new ArrayList<>();

        line = new Line();
        line.setColor(ChartUtils.COLORS[0]);        //设置线的颜色
        line.setShape(ValueShape.CIRCLE);                 //设置点的形状
        line.setHasLines(true);               //设置是否显示线
        line.setHasPoints(false);             //设置是否显示节点
        line.setCubic(false);                     //设置线是否立体或其他效果
        line.setFilled(false);                   //设置是否填充线下方区域

        axisX = new Axis();               //X轴
        axisY = new Axis();               //Y轴
        axisX.setTextColor(Color.GRAY);             //X轴灰色
        axisY.setTextColor(Color.GRAY);             //Y轴灰色
        axisX.setName("Axis X");                //设置名称
        axisY.setName("Axis Y");

        axisX.setHasLines(false);                // 网格线是否显示
        axisY.setHasLines(false);
        axisY.setLineColor(Color.BLACK);            // 网格线的颜色

        firstLoadData();
    }


    /**
     * 初次加载数据
     */
    void firstLoadData() {

        List<AxisValue> axisValues = new ArrayList<>();
        for(int i = 0; i < 100; i+= 10){
            AxisValue value = new AxisValue(i);
            String label = String.valueOf(i);
            value.setLabel(label);
            axisValues.add(value);
        }
        axisY.setValues(axisValues);

        Random random = new Random();
        for (int j = 0; j < 5; ++j) {
            values.add(new PointValue(j * 10,j * 10));
        }
        line.setValues(values);

        initData();
    }

    /**
     * 更新数据
     */
    void notifyData() {
        line.setValues(values);
        initData();
    }

    /**
     * 加载曲线数据
     */
    void initData() {
        List<Line> lines = new ArrayList<>();

        lines.add(line);

        lineData = new LineChartData(lines);
        lineData.setBaseValue(Float.NEGATIVE_INFINITY);           //设置基准数(大概是数据范围)

        lineData.setAxisXBottom(axisX);            //设置X轴位置 下方
        lineData.setAxisYLeft(axisY);              //设置Y轴位置 左边

        chartView.setLineChartData(lineData);
    }


}
